package org.jeecg.modules.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.oms.entity.OmsInquiryItem;
import org.jeecg.modules.oms.service.IOmsInquiryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 询单项管理
 */
@Api(tags = "询单项管理")
@RestController
@RequestMapping("/oms/omsInquiryItem")
@Slf4j
public class OmsInquiryItemController extends JeecgController<OmsInquiryItem, IOmsInquiryItemService> {

    @Autowired
    private IOmsInquiryItemService inquiryItemService;

    /**
     * 根据询单ID获取询单项列表
     *
     * @param inquiryId 询单ID
     * @return 询单项列表
     */
    @AutoLog(value = "询单项-根据询单ID获取列表")
    @ApiOperation(value = "询单项-根据询单ID获取列表", notes = "询单项-根据询单ID获取列表")
    @GetMapping(value = "/listByInquiryId")
    public Result<List<OmsInquiryItem>> listByInquiryId(@RequestParam(name = "inquiryId") String inquiryId) {
        try {
            List<OmsInquiryItem> list = inquiryItemService.getByInquiryId(inquiryId);
            return Result.OK(list);
        } catch (Exception e) {
            log.error("根据询单ID获取询单项列表失败", e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 批量保存询单项
     *
     * @param inquiryItems 询单项列表
     * @return 保存结果
     */
    @AutoLog(value = "询单项-批量保存")
    @ApiOperation(value = "询单项-批量保存", notes = "询单项-批量保存")
    @PostMapping(value = "/saveBatch")
    public Result<String> saveBatch(@RequestBody List<OmsInquiryItem> inquiryItems) {
        try {
            if (inquiryItems != null && !inquiryItems.isEmpty()) {
                // 先删除该询单的所有项
                String inquiryId = inquiryItems.get(0).getInquiryId();
                inquiryItemService.deleteByInquiryId(inquiryId);
                
                // 批量保存新的询单项
                inquiryItemService.saveBatch(inquiryItems);
            }
            return Result.OK("保存成功！");
        } catch (Exception e) {
            log.error("批量保存询单项失败", e);
            return Result.error("保存失败：" + e.getMessage());
        }
    }
}
